import pandas as pd
from docx import Document
from docx.shared import Inches

# 数据
data = {
    '用人部门': ['部门1', '部门2', '部门3', '部门4', '部门5', '部门6', '部门7'],
    '应聘者1': [3.25, 3.5, 2.5, 1.25, 1, 2.25, 1.75],
    '应聘者2': [1.63, 3.88, 2.88, 1.63, 1.38, 1.63, 1.13],
    '应聘者3': [3.13, 2.38, 1.38, 0.13, -0.12, 1.13, 0.63],
    '应聘者4': [2.46, 3.71, 2.71, 1.46, 1.21, 2.46, 1.96],
    '应聘者5': [2.84, 3.09, 2.09, 0.84, 0.59, 1.84, 1.34],
    '应聘者6': [1.34, 1.59, 0.59, 2.34, 2.09, 1.34, 0.84],
    '应聘者7': [2.66, 2.91, 1.91, 0.66, 0.41, 1.66, 1.16],
    '应聘者8': [2.16, 2.41, 1.41, 1.16, 0.91, 1.16, 0.66],
    '应聘者9': [2.16, 2.41, 1.41, 2.16, 1.91, 2.16, 1.66],
    '应聘者10': [0.66, 1.91, 0.91, 1.66, 1.41, 1.66, 1.16],
    '应聘者11': [1.54, 0.79, -0.21, 2.54, 2.29, 2.54, 2.04],
    '应聘者12': [2.99, 2.24, 1.24, 0.99, 0.74, 1.99, 1.49],
    '应聘者13': [2.37, 0.62, -0.38, 1.37, 1.12, 1.37, 0.87],
    '应聘者14': [0.87, 1.12, 0.12, 1.87, 1.62, 1.87, 1.37],
    '应聘者15': [2.31, 2.56, 1.56, 0.31, 0.06, 1.31, 0.81],
    '应聘者16': [2.25, 2.5, 1.5, 0.25, 0, 1.25, 0.75]
}

# 将数据转换为DataFrame
df_match = pd.DataFrame(data)

# 转置DataFrame
df_match = df_match.set_index('用人部门').T.reset_index()

# 创建一个Word文档
doc = Document()
doc.add_heading('最佳匹配度矩阵', level=1)

# 添加表格到文档
table = doc.add_table(rows=df_match.shape[0] + 1, cols=df_match.shape[1])

# 设置列宽（以英寸为单位）
column_widths = [Inches(1.0)] * df_match.shape[1]
for col_index, width in enumerate(column_widths):
    for row in table.rows:
        cell = row.cells[col_index]
        cell.width = width

# 设置行高（以点为单位）
row_height = 0.5  # 0.5英寸的行高，大约为36点
for row in table.rows:
    row.height = Inches(row_height)

# 添加表头
hdr_cells = table.rows[0].cells
for i, col_name in enumerate(df_match.columns):
    hdr_cells[i].text = col_name

# 添加表格数据
for i, row in df_match.iterrows():
    row_cells = table.add_row().cells
    for j, value in enumerate(row):
        row_cells[j].text = str(value)

# 保存文档
doc.save('最佳匹配度矩阵2.docx')
